﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SecOsctrl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked == false)
            {
                if (checkBox6.Checked == false)
                {
                    checkBox3.Checked = false;
                }
                else
                {
                    checkBox3.CheckState = CheckState.Indeterminate;
                }
            }
            else
            {
                if (checkBox6.Checked)
                {

                    checkBox3.Checked = true;
                    checkBox3.CheckState = CheckState.Checked;
                }
                else
                {
                    checkBox3.CheckState = CheckState.Indeterminate;
                }
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox6.Checked == false)
            {
                if (checkBox5.Checked == false)
                {
                    checkBox3.Checked = false;
                }
                else
                {
                    checkBox3.CheckState = CheckState.Indeterminate;
                }
            }
            else
            {
                if (checkBox5.Checked)
                {
                    checkBox3.Checked = true;
                    checkBox3.CheckState = CheckState.Checked;
                }
                else
                {
                    checkBox3.CheckState = CheckState.Indeterminate;
                }
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked && checkBox3.CheckState != CheckState.Indeterminate)
            {
                
                checkBox5.Checked = true;
                checkBox6.Checked = true;
                checkBox3.CheckState = CheckState.Checked;
            }
            else
            {
                checkBox5.Checked = false;
                checkBox6.Checked = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Login log = new Login();
            log.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "1MF-89S-WS8-MM456")
            {
                MessageBox.Show("The code is correct enjoy the savety!");
                textBox1.Enabled = false;
                button3.Enabled = false;
                button1.Enabled = true;
            }
            else
            {
                MessageBox.Show("The code is incorrect check on type faults or get a code by your system");
            }
        }
        FolderBrowserDialog f1 = new FolderBrowserDialog();
        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            f1.ShowNewFolderButton = false;
            if (f1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (Directory.Exists(f1.SelectedPath + @"\UserSettings") && Directory.Exists(f1.SelectedPath + @"\Settings"))
                {
                    File.WriteAllText(f1.SelectedPath + @"\UserSettings\password.SC",Base64Encode(SecOsctrl.Properties.Settings.Default.Password));
                    progressBar1.Value = 10;
                    File.WriteAllText(f1.SelectedPath + @"\UserSettings\name.SC", Base64Encode(SecOsctrl.Properties.Settings.Default.Name));
                    progressBar1.Value = 20;
                    File.WriteAllText(f1.SelectedPath + @"\Settings\internet.bool", checkBox2.Checked.ToString());
                    progressBar1.Value = 30;
                    File.WriteAllText(f1.SelectedPath + @"\Settings\Alarm.bool", checkBox4.Checked.ToString());
                    progressBar1.Value = 40;
                    File.WriteAllText(f1.SelectedPath + @"\Settings\Changer\TH.bool", checkBox5.Checked.ToString());
                    progressBar1.Value = 50;
                    File.WriteAllText(f1.SelectedPath + @"\Settings\Changer\MS.bool", checkBox6.Checked.ToString());
                    progressBar1.Value = 60;
                    File.WriteAllText(f1.SelectedPath + @"\Settings\internet\Port.int", numericUpDown1.Value.ToString());

                    File.Encrypt(f1.SelectedPath + @"\UserSettings\password.SC");
                    MessageBox.Show("All settings are apllied to the SD-Card");
                    progressBar1.Value = 0;
                }
                else
                {
                    Directory.CreateDirectory(f1.SelectedPath + @"\UserSettings");
                    progressBar1.Value = 2;
                    Directory.CreateDirectory(f1.SelectedPath + @"\Settings");
                    progressBar1.Value = 5;
                    Directory.CreateDirectory(f1.SelectedPath + @"\Settings\Changer");
                    Directory.CreateDirectory(f1.SelectedPath + @"\Settings\internet");
                    Directory.CreateDirectory(f1.SelectedPath + @"\Plugins");
                    progressBar1.Value = 7;

                    using (StreamWriter sw = File.CreateText(f1.SelectedPath + @"\UserSettings\password.SC"))
                    {
                        sw.Write(Base64Encode(SecOsctrl.Properties.Settings.Default.Password));
                    }
                    File.Encrypt(f1.SelectedPath + @"\UserSettings\password.SC");

                    progressBar1.Value = 10;
                    using (StreamWriter sw = File.CreateText(f1.SelectedPath + @"\UserSettings\name.SC"))
                    {
                        sw.Write(Base64Encode(SecOsctrl.Properties.Settings.Default.Name));
                    }

                    progressBar1.Value = 20;
                    using (StreamWriter sw = File.CreateText(f1.SelectedPath + @"\Settings\internet.bool"))
                    {
                        sw.Write(checkBox2.Checked.ToString());
                    }

                    progressBar1.Value = 30;
                    using(StreamWriter sw = File.CreateText(f1.SelectedPath + @"\Settings\Alarm.bool"))
                    {
                        sw.Write(checkBox4.Checked);
                    }

                    progressBar1.Value = 40;
                    using (StreamWriter sw = File.CreateText(f1.SelectedPath + @"\Settings\Changer\TH.bool"))
                    {
                        sw.Write(checkBox5.Checked);
                    }
                    progressBar1.Value = 50;
                    using (StreamWriter sw = File.CreateText(f1.SelectedPath + @"\Settings\Changer\MS.bool"))
                    {
                        sw.Write(checkBox6.Checked);
                    }
                    progressBar1.Value = 60;
                    using (StreamWriter sw = File.CreateText(f1.SelectedPath + @"\Settings\internet\Port.int"))
                    {
                        sw.Write(numericUpDown1.Value);
                    }
                    progressBar1.Value = 70;
                    MessageBox.Show("All settings are apllied to the SD-Card");
                    progressBar1.Value = 0;
                }
            }
        }

        
    }
}
